package com.dimanche.library_db;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

import com.dimanche.library_base.utils.AppUtils;
import com.dimanche.library_db.dao.DirDao;
import com.dimanche.library_db.dao.UserDao;
import com.dimanche.library_db.entity.CourseEntity;
import com.dimanche.library_db.entity.DetailedCourseEntity;
import com.dimanche.library_db.entity.NoteEntity;
import com.dimanche.library_db.entity.UserCourseEntity;
import com.dimanche.library_db.entity.UserEntity;
import com.dimanche.library_db.entity.WordEntity;

/**
 * author : Dimanche
 * date   : 2021/3/8 17:05
 * desc   : room数据库类，在该文件中配置表、dao以及升级数据库
 * version: 1.0
 */
@Database(entities = {UserEntity.class, CourseEntity.class, DetailedCourseEntity.class,
        NoteEntity.class, UserCourseEntity.class, WordEntity.class}, version = 1, exportSchema = true)
public abstract class AppDatabase extends RoomDatabase {
    private static final String DB_NAME = "LearnChinese";


    public abstract UserDao getUserDao();

    public abstract DirDao getDirDao();


    //单例
    public static AppDatabase getDatabase() {
        return Holder.instance;
    }

    private static class Holder {
        private static final AppDatabase instance = Room.databaseBuilder(AppUtils.getApplication(), AppDatabase.class, DB_NAME)
                // .allowMainThreadQueries()   //设置允许在主线程进行数据库操作，默认不允许
                // .fallbackToDestructiveMigration()  //设置数据库升级的时候清除之前的所有数据                                                       .addMigrations(MIGRATION_2_3)
                //.addMigrations(MIGRATION_1_2,MIGRATION_2_3,MIGRATION_1_3) //升级数据库
                .build();
    }


}
